package com.mall.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件工具类
 * 基于Spring Boot Mail实现
 */
@Slf4j
public class EmailUtil {

    /**
     * 发送简单文本邮件
     *
     * @param mailSender JavaMailSender实例
     * @param from       发件人邮箱
     * @param to         收件人邮箱
     * @param subject    邮件主题
     * @param content    邮件内容
     * @return 是否发送成功
     */
    public static boolean sendSimpleEmail(JavaMailSender mailSender, String from, String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);
            mailSender.send(message);
            log.info("简单邮件发送成功，收件人: {}", to);
            return true;
        } catch (Exception e) {
            log.error("发送简单邮件失败，收件人: {}", to, e);
            return false;
        }
    }

    /**
     * 发送HTML格式邮件
     *
     * @param mailSender JavaMailSender实例
     * @param from       发件人邮箱
     * @param to         收件人邮箱
     * @param subject    邮件主题
     * @param content    邮件内容（HTML格式）
     * @return 是否发送成功
     */
    public static boolean sendHtmlEmail(JavaMailSender mailSender, String from, String to, String subject, String content) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
            log.info("HTML邮件发送成功，收件人: {}", to);
            return true;
        } catch (MessagingException e) {
            log.error("发送HTML邮件失败，收件人: {}", to, e);
            return false;
        }
    }

    /**
     * 发送带附件的邮件
     *
     * @param mailSender JavaMailSender实例
     * @param from       发件人邮箱
     * @param to         收件人邮箱
     * @param subject    邮件主题
     * @param content    邮件内容
     * @return 是否发送成功
     */
    public static boolean sendAttachmentsEmail(JavaMailSender mailSender, String from, String to, String subject, String content) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            mailSender.send(message);
            log.info("带附件邮件发送成功，收件人: {}", to);
            return true;
        } catch (MessagingException e) {
            log.error("发送带附件邮件失败，收件人: {}", to, e);
            return false;
        }
    }
}